package chapter03
import org.apache.log4j.{Level, Logger}
import org.apache.spark.{SparkConf, SparkContext}
object Test25_combineByKey {
  def main(args: Array[String]): Unit = {
    Logger.getLogger("org.apache.spark").setLevel(Level.WARN)
    val conf = new SparkConf().setMaster("local[*]").setAppName("combineByKey")
    val sc = new SparkContext(conf)
    val value = sc.makeRDD(List(("a", 88), ("b", 95), ("a", 91),
      ("b", 93), ("a", 95), ("b", 98)))
    //计算每个键的平均值 (key,sum,count)
    val value1 = value.combineByKey(
      v => (v, 1),
      (acc: (Int, Int), v) => (acc._1 + v, acc._2 + 1),
      (acc1: (Int, Int), acc2: (Int, Int)) => (acc1._1 + acc2._1, acc1._2 + acc2._2)
    )
    println(value1.collect().toList)
    val value2 = value1.map(e => (e._1, e._2._1.toDouble / e._2._2))
    println(value2.collect().toList)
  }
}
